<?php

class Gioithieu extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->library('form_validation');
        $this->load->helper(array('form', 'url', 'codegen_helper'));
        $this->load->model('codegen_model', '', TRUE);
        $this->load->helper('ckeditor');

        //Ckeditor's configuration
        $this->data['ckeditor'] = array(
            //ID of the textarea that will be replaced
            'id' => 'content',
            'path' => 'js/ckeditor',
            //Optionnal values
            'config' => array(
                'toolbar' => "Full", //Using the Full toolbar
                'width' => "550px", //Setting a custom width
                'height' => '100px', //Setting a custom height
            ),
            //Replacing styles from the "Styles tool"
            'styles' => array(
                //Creating a new style named "style 1"
                'style 1' => array(
                    'name' => 'Blue Title',
                    'element' => 'h2',
                    'styles' => array(
                        'color' => 'Blue',
                        'font-weight' => 'bold'
                    )
                ),
                //Creating a new style named "style 2"
                'style 2' => array(
                    'name' => 'Red Title',
                    'element' => 'h2',
                    'styles' => array(
                        'color' => 'Red',
                        'font-weight' => 'bold',
                        'text-decoration' => 'underline'
                    )
                )
            )
        );

        $this->data['ckeditor_2'] = array(
            //ID of the textarea that will be replaced
            'id' => 'noidung',
            'path' => 'js/ckeditor',
            //Optionnal values
            'config' => array(
                'width' => "550px", //Setting a custom width
                'height' => '100px', //Setting a custom height
                'toolbar' => array(//Setting a custom toolbar
                    array('Bold', 'Italic'),
                    array('Underline', 'Strike', 'FontSize'),
                    array('Smiley'),
                    '/'
                )
            ),
            //Replacing styles from the "Styles tool"
            'styles' => array(
                //Creating a new style named "style 1"
                'style 3' => array(
                    'name' => 'Green Title',
                    'element' => 'h3',
                    'styles' => array(
                        'color' => 'Green',
                        'font-weight' => 'bold'
                    )
                )
            )
        );
    }

    function index() {
        $this->manage();
    }

    function manage() {
        $this->load->library('table');
        $this->load->library('pagination');

        //paging
        $config['base_url'] = base_url() . 'index.php/gioithieu/manage/';
        $config['total_rows'] = $this->codegen_model->count('gioithieu');
        $config['per_page'] = 3;
        $this->pagination->initialize($config);
        // make sure to put the primarykey first when selecting , 
        //eg. 'userID,name as Name , lastname as Last_Name' , Name and Last_Name will be use as table header.
        // Last_Name will be converted into Last Name using humanize() function, under inflector helper of the CI core.
        $this->data['results'] = $this->codegen_model->get('gioithieu', 'gioithieu_id,noidung,hinh_gioithieu_url,trangthai', '', $config['per_page'], $this->uri->segment(3));

        $this->load->view('admin/gioithieu/gioithieu_list', $this->data);
        //$this->template->load('content', 'gioithieu_list', $this->data); // if have template library , http://maestric.com/doc/php/codeigniter_template
    }

    function add() {
        $this->load->library('form_validation');
        $this->data['custom_error'] = '';

        if ($this->form_validation->run('gioithieu') == false) {
            $this->data['custom_error'] = (validation_errors() ? '<div class="form_error">' . validation_errors() . '</div>' : false);
        } else {
            $data = array(
                'noidung' => set_value('noidung'),
                'hinh_gioithieu_url' => set_value('hinh_gioithieu_url'),
                'trangthai' => set_value('trangthai')
            );

            if ($this->codegen_model->add('gioithieu', $data) == TRUE) {
                //$this->data['custom_error'] = '<div class="form_ok"><p>Added</p></div>';
                // or redirect
                redirect(base_url() . 'index.php/gioithieu/manage/');
            } else {
                $this->data['custom_error'] = '<div class="form_error"><p>An Error Occured.</p></div>';
            }
        }
        $this->load->view('admin/gioithieu/gioithieu_add', $this->data);
        //$this->template->load('content', 'gioithieu_add', $this->data);
    }

    function edit() {
        $this->load->library('form_validation');
        $this->load->library('upload');
        
        $this->data['custom_error'] = '';

        if ($this->form_validation->run('gioithieu') == false) {
            $this->data['custom_error'] = (validation_errors() ? '<div class="form_error">' . validation_errors() . '</div>' : false);
        } else {
            
            $name = $_FILES['hinh_gioithieu_url']['name'];
            if (isset($_FILES)) {

                if (!empty($name)) {

                    // Specify configuration for File 1
                    $config['upload_path'] = 'upload/gioithieuimg/';

                    $config['allowed_types'] = 'gif|jpg|png';
                    $config['max_size'] = '10000';
                    //                $config['max_width'] = '5000';
                    //                $config['max_height'] = '5000';
                    // Initialize config for File 1
                    $this->upload->initialize($config);

                    if ($this->upload->do_upload('hinh_gioithieu_url')) {

                        $data = $this->upload->data();
                    } else {
                        echo $this->upload->display_errors();
                    }
                }
            }
            
            $fckeditor->Value = htmlentities($this->input->post('noidung', ENT_NOQUOTES, "UTF-8", TRUE));


            if ($name != "") {
                $data = array(
                    'noidung' => $fckeditor->Value,
                    'hinh_gioithieu_url' => $name,
                    'trangthai' => $this->input->post('trangthai')
                );
            } else {
                $data = array(
                    'noidung' => $fckeditor->Value,
                    'trangthai' => $this->input->post('trangthai')
                );
            }

            if ($this->codegen_model->edit('gioithieu', $data, 'gioithieu_id', $this->input->post('gioithieu_id')) == TRUE) {
                redirect(base_url() . 'index.php/gioithieu/manage/');
            } else {
                $this->data['custom_error'] = '<div class="form_error"><p>An Error Occured</p></div>';
            }
        }

        $this->data['result'] = $this->codegen_model->get('gioithieu', 'gioithieu_id,noidung,hinh_gioithieu_url,trangthai', 'gioithieu_id = ' . $this->uri->segment(3), NULL, NULL, true);

        $this->load->view('admin/gioithieu/gioithieu_edit', $this->data);
        //$this->template->load('content', 'gioithieu_edit', $this->data);
    }

    function delete() {
        $ID = $this->uri->segment(3);
        $this->codegen_model->delete('gioithieu', 'gioithieu_id', $ID);
        redirect(base_url() . 'index.php/gioithieu/manage/');
    }

}

/* End of file gioithieu.php */
/* Location: ./system/application/controllers/gioithieu.php */